package qqmeeting

import (
	"encoding/json"
	"errors"
	"fmt"
)

//CreateMeetingBody 创建会议提交参数
type CreateMeetingBody struct {
	Userid     string                 `json:"userid"`
	Instanceid int                    `json:"instanceid"`
	Subject    string                 `json:"subject"`
	Type       int                    `json:"type"`
	Hosts      []UserObj              `json:"hosts"`
	Invitees   []UserObj              `json:"invitees"`
	StartTime  string                 `json:"start_time"`
	EndTime    string                 `json:"end_time"`
	Password   string                 `json:"password"`
	Settings   map[string]interface{} `json:"settings"`
}

//UserObj 用户对象
type UserObj struct {
	Userid       string `json:"userid"`
	IsAnonymous  bool   `json:"is_anonymous"`
	NickName     string `json:"nickname"`
	ProfilePhoto string `json:"profile_photo"`
}

//CreateMeetingResponse 创建会议响应结果
type CreateMeetingResponse struct {
	Response
	MeetingNumber   int           `json:"meeting_number"`
	MeetingInfoList []MeetingInfo `json:"meeting_info_list"`
}

//MeetingInfo 会议对象
type MeetingInfo struct {
	Subject      string                 `json:"subject"`
	MeetingID    string                 `json:"meeting_id"`
	MeetingCode  string                 `json:"meeting_code"`
	Password     string                 `json:"password"`
	StartTime    string                 `json:"start_time"`
	EndTime      string                 `json:"end_time"`
	Hosts        []UserObj              `json:"hosts"`
	Participants []UserObj              `json:"participants"`
	JoinURL      string                 `json:"join_url"`
	Settings     map[string]interface{} `json:"settings"`
}

//CreateMeeting 创建会议
func (c *Client) CreateMeeting(body map[string]interface{}) (resp CreateMeetingResponse, err error) {
	uri := "/v1/meetings"
	var statusCode int
	var content []byte
	if statusCode, content, err = c.POST(uri, body); err != nil {
		return
	}
	if content != nil {
		if err = json.Unmarshal(content, &resp); err != nil {
			err = errors.New(err.Error() + "\n" + string(content))
		}
	} else {
		err = fmt.Errorf("[%d] Bad request", statusCode)
	}
	resp.StatusCode = statusCode
	return
}
